/*
 * Copyright 2010 jun.enomoto<jun.enomoto@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package foursquare4j.xml.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import foursquare4j.type.Beenhere;
import foursquare4j.type.Checkin;
import foursquare4j.type.Checkins;
import foursquare4j.type.Mayor;
import foursquare4j.type.Special;
import foursquare4j.type.Specials;
import foursquare4j.type.Stats;
import foursquare4j.type.Tags;
import foursquare4j.type.Tip;
import foursquare4j.type.Tips;
import foursquare4j.type.User;
import foursquare4j.type.Venue;

public class VenueHandler extends Handler<Venue> {

	private static enum E {
		_, STATS, STATS_BEENHERE, STATS_MAYOR, STATS_MAYOR_USER, CHECKINS, CHECKINS_CHECKIN, CHECKINS_CHECKIN_USER, TIPS, TIPS_TIP, TIPS_TIP_USER, TAGS, SPECIALS, SPECIALS_SPECIAL, SPECIALS_SPECIAL_VENUE
	}

	private E e;

	private Venue venue;
	private Checkin checkin;
	private Tip tip;
	private Special special;

	@Override
	public void startDocument() throws SAXException {
		venue = new Venue();
	}

	@Override
	public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if ("venue".equals(qName) && e == null) e = E._;
		else if ("stats".equals(qName) && e == E._) {
			e = E.STATS;
			venue.stats = new Stats();
		} else if ("beenhere".equals(qName) && e == E.STATS) {
			e = E.STATS_BEENHERE;
			venue.stats.beenhere = new Beenhere();
		} else if ("mayor".equals(qName) && e == E.STATS) {
			e = E.STATS_MAYOR;
			venue.stats.mayor = new Mayor();
		} else if ("user".equals(qName) && e == E.STATS_MAYOR) {
			e = E.STATS_MAYOR_USER;
			venue.stats.mayor.user = new User();
		} else if ("checkins".equals(qName) && e == E._) {
			e = E.CHECKINS;
			venue.checkins = new Checkins();
		} else if ("checkin".equals(qName) && e == E.CHECKINS) {
			e = E.CHECKINS_CHECKIN;
			venue.checkins.add(checkin = new Checkin());
		} else if ("user".equals(qName) && e == E.CHECKINS_CHECKIN) {
			e = E.CHECKINS_CHECKIN_USER;
			checkin.user = new User();
		} else if ("tips".equals(qName) && e == E._) {
			e = E.TIPS;
			venue.tips = new Tips();
		} else if ("tip".equals(qName) && e == E.TIPS) {
			e = E.TIPS_TIP;
			venue.tips.add(tip = new Tip());
		} else if ("user".equals(qName) && e == E.TIPS_TIP) {
			e = E.TIPS_TIP_USER;
			tip.user = new User();
		} else if ("tags".equals(qName) && e == E._) {
			e = E.TAGS;
			venue.tags = new Tags();
		} else if ("specials".equals(qName) && e == E._) {
			e = E.SPECIALS;
			venue.specials = new Specials();
		} else if ("special".equals(qName) && e == E.SPECIALS) {
			e = E.SPECIALS_SPECIAL;
			venue.specials.add(special = new Special());
		} else if ("venue".equals(qName) && e == E.SPECIALS_SPECIAL) {
			e = E.SPECIALS_SPECIAL_VENUE;
			special.venue = new Venue();
		}
	}

	@Override
	public void endElement(final String uri, final String localName, final String qName) throws SAXException {
		if ("venue".equals(qName) && e == E._) e = null;
		else if ("stats".equals(qName) && e == E.STATS) e = E._;
		else if ("beenhere".equals(qName) && e == E.STATS_BEENHERE) e = E.STATS;
		else if ("mayor".equals(qName) && e == E.STATS_MAYOR) e = E.STATS;
		else if ("user".equals(qName) && e == E.STATS_MAYOR_USER) e = E.STATS_MAYOR;
		else if ("checkins".equals(qName) && e == E.CHECKINS) e = E._;
		else if ("checkin".equals(qName) && e == E.CHECKINS_CHECKIN) e = E.CHECKINS;
		else if ("user".equals(qName) && e == E.CHECKINS_CHECKIN_USER) e = E.CHECKINS_CHECKIN;
		else if ("tips".equals(qName) && e == E.TIPS) e = E._;
		else if ("tip".equals(qName) && e == E.TIPS_TIP) e = E.TIPS;
		else if ("user".equals(qName) && e == E.TIPS_TIP_USER) e = E.TIPS_TIP;
		else if ("tags".equals(qName) && e == E.TAGS) e = E._;
		else if ("specials".equals(qName) && e == E.SPECIALS) e = E._;
		else if ("special".equals(qName) && e == E.SPECIALS_SPECIAL) e = E.SPECIALS;
		else if ("venue".equals(qName) && e == E.SPECIALS_SPECIAL_VENUE) e = E.SPECIALS_SPECIAL;
	}

	@Override
	protected void characters(final String text) throws SAXException {
		switch (e) {
			case _:
				set(venue, tag, text);
				break;
			case STATS:
				set(venue.stats, tag, text);
				break;
			case STATS_BEENHERE:
				set(venue.stats.beenhere, tag, text);
				break;
			case STATS_MAYOR:
				set(venue.stats.mayor, tag, text);
				break;
			case STATS_MAYOR_USER:
				set(venue.stats.mayor.user, tag, text);
				break;
			case CHECKINS:
				break;
			case CHECKINS_CHECKIN:
				set(checkin, tag, text);
				break;
			case CHECKINS_CHECKIN_USER:
				set(checkin.user, tag, text);
				break;
			case TIPS:
				break;
			case TIPS_TIP:
				set(tip, tag, text);
				break;
			case TIPS_TIP_USER:
				set(tip.user, tag, text);
				break;
			case TAGS:
				set(venue.tags, tag, text);
				break;
			case SPECIALS:
				break;
			case SPECIALS_SPECIAL:
				set(special, tag, text);
				break;
			case SPECIALS_SPECIAL_VENUE:
				set(special.venue, tag, text);
				break;
		}
	}

	@Override
	public Venue getObject() {
		return venue;
	}
}